package com.mexico.inloancash.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.mexico.inloancash.R;
import com.mexico.inloancash.bean.BaseBean;
import com.mexico.inloancash.http.IndiaRequestApi;
import com.mexico.inloancash.http.IndiaDKFactory;
import com.mexico.inloancash.utils.MyToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class India_MyBank_Info_Activity extends BaseActivity {

    @BindView(R.id.logon_back)
    ImageView mIndiaGonBack;

    @BindView(R.id.title)
    TextView mIndiaTvTitle;

    @BindView(R.id.bank_name)
    TextView mIndiaTvBankName;

    @BindView(R.id.bank_count)
    TextView mIndiaTvBankCount;

    @BindView(R.id.detail_bank)
    LinearLayout mIndiaLlBanks;

    @BindView(R.id.unBind)
    Button mIndiaBtnUnBind;

    private String mBankId;//id
    private String mBank;//银行卡名字
    private String mBankNo;//银行卡号
    private int mRandom;//随机数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.india_activity_mine_view_bank_layout);
        ButterKnife.bind(this);
        mIndiaTvTitle.setText("Kartu Bank Saya");
        mBankId = getIntent().getStringExtra("id");
        mBank = getIntent().getStringExtra("bank");
        mBankNo = getIntent().getStringExtra("bankno");
        mRandom = getIntent().getIntExtra("radom",-1);
        mIndiaTvBankName.setText(mBank);
        String str = mBankNo.substring(mBankNo.length() - 4 );
        mIndiaTvBankCount.setText("**** **** **** " + str);
        if(mRandom == 1){
            mIndiaLlBanks.setBackgroundResource(R.mipmap.india_add_picture_bankcard_pic_2bg);
        }else {
            mIndiaLlBanks.setBackgroundResource(R.mipmap.india_add_picture_bankcard_pic_1bg);
        }
    }

    @OnClick({R.id.logon_back, R.id.unBind})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.logon_back:
                finish();
                break;
            case R.id.unBind:
                indiaBankInfo();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    public void indiaBankInfo(){
        loading.show();
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> delete = api.deleteBank(mBankId);
        delete.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful()){
                    BaseBean baseBean = new Gson().fromJson(response.body(),BaseBean.class);
                    if(baseBean.getCode() == 200){
                        setResult(0);
                        MyToast.show(India_MyBank_Info_Activity.this,"Berhasil diangkat");
                        finish();
                    }else {
                        MyToast.show(India_MyBank_Info_Activity.this,baseBean.getMsg());
                    }
                }
                loading.dismiss();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loading.dismiss();
            }
        });
    }
}
